//
//  JCLEmoticon.swift
//  JCLEmoticons
//
//  Created by DDC on 2017/9/25.
//  Copyright © 2017年 DDC. All rights reserved.
//

import UIKit
import YYModel
class JCLEmoticon: NSObject {
    
    var type = false
    var chs:String?
    var png:String?
    
    /// emoji十六进制
    var code:String? {
        didSet {
            guard let code = code else {
                return
            }
            let scanner = Scanner(string: code)
            var result:UInt32 = 0
            scanner.scanHexInt32(&result)
            emojiStr = String(Character(UnicodeScalar(result)!))
        }
    }
    /// emoji字符串
    var emojiStr:String?
    var directory:String?
    
    /// 记录emoji表情的使用次数
    var times:Int = 0
    
    
    var image:UIImage? {
        
        if type {
            
            return nil
        }
        guard let directory = directory,
                  let png = png,
                  let path = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil),
                    let bundle = Bundle(path: path)
            else {
                return nil
        }
        return UIImage(named: "\(directory)/\(png)", in: bundle, compatibleWith: nil)
    }

    override var description: String {
        
        return yy_modelDescription()
    }
}

extension JCLEmoticon {
    func imageText(textFont:UIFont) -> NSAttributedString {
        
        guard let image = image else {
            
            return NSAttributedString(string: "")
        }
        let attachment = JCLEmoticonAttachment()
        attachment.chs = chs
        let height:CGFloat = textFont.lineHeight
        attachment.image = image
        attachment.bounds = CGRect(x: 0, y: -4, width: height, height: height)
        
        return NSAttributedString(attachment: attachment)
    }
}
